//
//  ElectricalCertificateView.swift
//  ezyform
//
//  Created by MoennigQ on 11/9/20.
//  Copyright © 2020 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit
import SnapKit
class ElectricalCertificateView: GreenNaviBarView {
    
    let previewBtn = UIButton()
    let scrollView = DIYScrollView()
    //
    let installationCheckbox = CheckboxButton(title: "TESTING AND COMPLIANCE (Electrical installations)")
    let equipmentCheckbox = CheckboxButton(title: "TESTING AND SAFETY (Electrical equipment)")
    //
    let nameTitleTextfield = TextFieldWithRequiredView()//Mrs or Miss
    let givenNameTextfield = TextFieldWithRequiredView()
    let surnameTextfield = TextFieldWithRequiredView()
    //
    let streetTextfield = TextFieldWithRequiredView()
    let suburbTextfield = TextFieldWithRequiredView()
    let postcodeTextfield = TextFieldWithRequiredView()
    //
    let electricalTestTextView = TextViewWithRequiredView()
    let dateOfTestTextfield = TextFieldWithRequiredView()
    let dateOfTestCalendarBtn = UIButton()
    let electricalContractorLicenceTextfield = TextFieldWithRequiredView()
    let nameOnContractorLicenceTextfield = DIYTextField()
    let phoneOnContractorLicenceTextfield = DIYTextField()
    //
    let electricianNameTextfield = DIYTextField()
    let signDateTextfield = TextFieldWithRequiredView()
    let dateCalendarBtn = UIButton()
    let signatureView = SignatureView()
    let clearSignatureBtn = BaseButton()
    //
    let cancelBtn = OperationButton(R.string.localizable.cancel(),backColor: UIColor(hexStr: "#70798A"))
    let addBtn = OperationButton(R.string.localizable.add())
    
    override func setUpView() {
        let titleFont = R.font.interSemiBold(size: 13)
        let tipFont = R.font.interMedium(size: 13)
        //preview Btn
        previewBtn.setImage(R.image.electrical_certificate_preview_tabview_icon(), for: UIControl.State.normal)
        topBackView.addSubview(previewBtn)
        previewBtn.snp.makeConstraints { make in
            make.right.equalToSuperview().offset(-8)
            make.width.height.equalTo(40)
            make.centerY.equalTo(backToPreBtn)
        }
        
        scrollView.backgroundColor = .white
        self.addSubview(scrollView)
        scrollView.snp.makeConstraints { (make) in
            make.top.equalTo(topBackView.snp.bottom).offset(-20)
            make.left.right.equalTo(self)
            make.bottom.equalTo(self.safeAreaLayoutGuide.snp.bottom)
        }
        
        let contentView = UIView()
        contentView.backgroundColor = .clear
        scrollView.addSubview(contentView)
        
        contentView.snp.makeConstraints { (make) in
            make.top.bottom.equalTo(scrollView)
            make.left.right.equalTo(self)
            make.height.greaterThanOrEqualTo(0)
        }
        let backView = UIView()
        backView.backgroundColor = .white
        contentView.addSubview(backView)
        backView.snp.makeConstraints { (make) in
            make.left.right.top.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
            make.bottom.equalToSuperview()
        }
        //
        let toptitleLabel = TitleLabelWithRequired("Indicates a mandatory field")
        toptitleLabel.titleLabel.font = titleFont
        backView.addSubview(toptitleLabel)
        toptitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(20)
            make.left.equalTo(16)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        let certificateOfLabel = ContentLabel()
        certificateOfLabel.text = "CERTIFICATE OF:"
        certificateOfLabel.font = R.font.interSemiBold(size: 14)
        backView.addSubview(certificateOfLabel)
        certificateOfLabel.snp.makeConstraints { (make) in
            make.top.equalTo(toptitleLabel.snp.bottom).offset(6)
            make.left.equalTo(toptitleLabel)
            make.right.equalTo(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let certificateOfTipLabel = ContentLabel(hexStr: "#292D3266")
        certificateOfTipLabel.text = "(Please mark relevant check-box)"
        certificateOfTipLabel.numberOfLines = 0
        certificateOfTipLabel.font = R.font.interMedium(size: 13)
        backView.addSubview(certificateOfTipLabel)
        certificateOfTipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(certificateOfLabel.snp.bottom)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(installationCheckbox)
        installationCheckbox.snp.makeConstraints { (make) in
            make.top.equalTo(certificateOfTipLabel.snp.bottom).offset(15)
            make.left.equalTo(certificateOfLabel)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(30)
        }
        //
        let installationTipLabel = ContentLabel(hexStr: "#292D3266")
        installationTipLabel.text = "Issued in accordance with s227 of the Electrical Safety Regulation 2013"
        installationTipLabel.numberOfLines = 0
        installationTipLabel.font = tipFont
        backView.addSubview(installationTipLabel)
        installationTipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(installationCheckbox.snp.bottom).offset(4)
            make.left.equalTo(certificateOfLabel).offset(30)
            make.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(equipmentCheckbox)
        equipmentCheckbox.snp.makeConstraints { (make) in
            make.top.equalTo(installationTipLabel.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.right.equalToSuperview().offset(-16)
            make.height.greaterThanOrEqualTo(0)
        }
        
        //
        let equipmentTipLabel = ContentLabel(hexStr: "#292D3266")
        equipmentTipLabel.text = "Issued in accordance with s26 of the Electrical Safety Regulation 2013"
        equipmentTipLabel.numberOfLines = 0
        equipmentTipLabel.font = tipFont
        backView.addSubview(equipmentTipLabel)
        equipmentTipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(equipmentCheckbox.snp.bottom).offset(4)
            make.left.equalTo(certificateOfLabel).offset(30)
            make.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let firstLineView = View(hexStr: Constant.SeparatorColorHexStr)
        backView.addSubview(firstLineView)
        firstLineView.snp.makeConstraints { make in
            make.top.equalTo(equipmentTipLabel.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.right.equalTo(-16)
            make.height.equalTo(1)
        }
        let workPerformTitleLabel = TitleLabelWithRequired("Work performed for:")
        workPerformTitleLabel.titleLabel.font = titleFont
        backView.addSubview(workPerformTitleLabel)
        workPerformTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(firstLineView.snp.bottom).offset(14)
            make.left.equalTo(toptitleLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        let nameTitleLabel = TitleLabelWithRequired("Name")
        nameTitleLabel.titleLabel.font = titleFont
        backView.addSubview(nameTitleLabel)
        nameTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(workPerformTitleLabel.snp.bottom).offset(6)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        nameTitleTextfield.textField.placeholder = "Title"
        backView.addSubview(nameTitleTextfield)
        nameTitleTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(nameTitleLabel.snp.bottom).offset(6)
            make.left.equalTo(certificateOfLabel)
            make.right.equalTo(firstLineView)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        givenNameTextfield.textField.placeholder = "First Name/Corporate Body name/Government body name/Trustee name"
        backView.addSubview(givenNameTextfield)
        givenNameTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(nameTitleTextfield.snp.bottom).offset(10)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        surnameTextfield.textField.placeholder = "Surname"
        backView.addSubview(surnameTextfield)
        surnameTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(givenNameTextfield.snp.bottom).offset(10)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //address
        let addressTitleLabel = TitleLabelWithRequired("Address")
        addressTitleLabel.titleLabel.font = titleFont
        backView.addSubview(addressTitleLabel)
        addressTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(surnameTextfield.snp.bottom).offset(20)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        streetTextfield.textField.placeholder = "Street"
        backView.addSubview(streetTextfield)
        streetTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(addressTitleLabel.snp.bottom).offset(12)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        suburbTextfield.textField.placeholder = "Suburb/town"
        backView.addSubview(suburbTextfield)
        suburbTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(streetTextfield.snp.bottom).offset(12)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        postcodeTextfield.textField.placeholder = "Postcode"
        backView.addSubview(postcodeTextfield)
        postcodeTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(suburbTextfield.snp.bottom).offset(12)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let secondLineView = View(hexStr: Constant.SeparatorColorHexStr)
        backView.addSubview(secondLineView)
        secondLineView.snp.makeConstraints { make in
            make.top.equalTo(postcodeTextfield.snp.bottom).offset(14)
            make.left.right.height.equalTo(firstLineView)
        }
        //
        let electricalTestLabel = TitleLabelWithRequired("Electrical installation / equipment tested:")
        electricalTestLabel.titleLabel.font = titleFont
        backView.addSubview(electricalTestLabel)
        electricalTestLabel.snp.makeConstraints { (make) in
            make.top.equalTo(secondLineView.snp.bottom).offset(14)
            make.left.right.equalTo(nameTitleTextfield)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let electricalTestTipLabel = ContentLabel(hexStr: "#292D3266")
        electricalTestTipLabel.text = "(please include site address for electrical installation work if different from above):"
        electricalTestTipLabel.numberOfLines = 0
        electricalTestTipLabel.font = tipFont
        backView.addSubview(electricalTestTipLabel)
        electricalTestTipLabel.snp.makeConstraints { (make) in
            make.top.equalTo(electricalTestLabel.snp.bottom).offset(4)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(electricalTestTextView)
        electricalTestTextView.snp.makeConstraints { (make) in
            make.top.equalTo(electricalTestTipLabel.snp.bottom).offset(4)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        let dateOfTestTitle = TitleLabelWithRequired("Date of test")
        dateOfTestTitle.titleLabel.font = titleFont
        backView.addSubview(dateOfTestTitle)
        dateOfTestTitle.snp.makeConstraints { (make) in
            make.top.equalTo(electricalTestTextView.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        dateOfTestTextfield.textField.isEnabled = false
        dateOfTestTextfield.textField.clearButtonMode = .never
        backView.addSubview(dateOfTestTextfield)
        dateOfTestTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(dateOfTestTitle.snp.bottom).offset(4)
            make.left.right.equalTo(firstLineView)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        dateOfTestCalendarBtn.setImage(UIImage(named: "electrical_certificate_date_sel"), for: UIControl.State.normal)
        backView.addSubview(dateOfTestCalendarBtn)
        dateOfTestCalendarBtn.snp.makeConstraints { (make) in
            make.centerY.right.equalTo(dateOfTestTextfield)
            make.width.height.equalTo(40)
        }
        // Electrical contractor licence number
        let electricalContractorLicenceTitle = TitleLabelWithRequired("Electrical contractor licence number")
        electricalContractorLicenceTitle.titleLabel.font = titleFont
        backView.addSubview(electricalContractorLicenceTitle)
        electricalContractorLicenceTitle.snp.makeConstraints { (make) in
            make.top.equalTo(dateOfTestTextfield.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(electricalContractorLicenceTextfield)
        electricalContractorLicenceTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(electricalContractorLicenceTitle.snp.bottom).offset(4)
            make.left.right.equalTo(firstLineView)
            make.height.greaterThanOrEqualTo(0)
        }
        //Name on contractor licence
        let nameOnContractorLicenceTitle = ContentLabel()
        nameOnContractorLicenceTitle.text = "Name on contractor licence"
        nameOnContractorLicenceTitle.font = titleFont
        backView.addSubview(nameOnContractorLicenceTitle)
        nameOnContractorLicenceTitle.snp.makeConstraints { (make) in
            make.top.equalTo(electricalContractorLicenceTextfield.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(nameOnContractorLicenceTextfield)
        nameOnContractorLicenceTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(nameOnContractorLicenceTitle.snp.bottom).offset(4)
            make.left.right.equalTo(firstLineView)
            make.height.equalTo(DIYTextField.height)
        }
        //Electrical contractor phone number
        let phoneOnContractorLicence = ContentLabel()
        phoneOnContractorLicence.text = "Electrical contractor phone number"
        phoneOnContractorLicence.font = titleFont
        backView.addSubview(phoneOnContractorLicence)
        phoneOnContractorLicence.snp.makeConstraints { (make) in
            make.top.equalTo(nameOnContractorLicenceTextfield.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(phoneOnContractorLicenceTextfield)
        phoneOnContractorLicenceTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(phoneOnContractorLicence.snp.bottom).offset(4)
            make.left.right.equalTo(firstLineView)
            make.height.equalTo(DIYTextField.height)
        }
        //tipForElectricalInstallationLabel
        let tipForElectricalInstallationLabel = BaseLabel()
        var subContent = "electrical installations"
        var content = "For " + subContent + ", this certifies that the electrical installation, to the extent it is affected by the electrical work, has been tested to ensure that it is electrically safe and is in accordance with the requirements of the wiring rules and any other standard applying under the Electrical Safety Regulation 2013 to the electrical installation."
        var range = (content as NSString).range(of: subContent)
        var attrString = NSMutableAttributedString(string: content)
        let attr1: [NSMutableAttributedString.Key: Any] = [.font: R.font.interMedium(size: 14) as Any,.foregroundColor: UIColor(hexStr: "#292D32")]
        let attr2: [NSMutableAttributedString.Key: Any] = [.font: R.font.interBold(size: 14) as Any,.foregroundColor: UIColor(hexStr: "#292D32")]
        attrString.addAttributes(attr1, range: NSRange(location: 0, length: content.count))
        attrString.addAttributes(attr2, range: NSRange(location: range.location, length: range.length))
        tipForElectricalInstallationLabel.attributedText = attrString
        tipForElectricalInstallationLabel.numberOfLines = 0
        backView.addSubview(tipForElectricalInstallationLabel)
        tipForElectricalInstallationLabel.snp.makeConstraints { (make) in
            make.top.equalTo(phoneOnContractorLicenceTextfield.snp.bottom).offset(20)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //tipForElectrical equipment
        let tipForElectricalEquipmentLabel = BaseLabel()
        subContent = "electrical equipment"
        content = "For " + subContent + ", this certifies that the electrical equipment, to the extent it is affected by the electrical work, is electrically safe."
        range = (content as NSString).range(of: subContent)
        attrString = NSMutableAttributedString(string: content)
        attrString.addAttributes(attr1, range: NSRange(location: 0, length: content.count))
        attrString.addAttributes(attr2, range: NSRange(location: range.location, length: range.length))
        tipForElectricalEquipmentLabel.attributedText = attrString
        tipForElectricalEquipmentLabel.numberOfLines = 0
        backView.addSubview(tipForElectricalEquipmentLabel)
        tipForElectricalEquipmentLabel.snp.makeConstraints { (make) in
            make.top.equalTo(tipForElectricalInstallationLabel.snp.bottom).offset(12)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //Name
        let nameTitle = ContentLabel()
        nameTitle.text = "Name"
        nameTitle.font = titleFont
        backView.addSubview(nameTitle)
        nameTitle.snp.makeConstraints { (make) in
            make.top.equalTo(tipForElectricalEquipmentLabel.snp.bottom).offset(20)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        backView.addSubview(electricianNameTextfield)
        electricianNameTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(nameTitle.snp.bottom).offset(4)
            make.left.right.equalTo(certificateOfLabel)
            make.height.equalTo(DIYTextField.height)
        }
        //date
        let dateTitle = ContentLabel()
        dateTitle.text = "Date"
        dateTitle.font = titleFont
        backView.addSubview(dateTitle)
        dateTitle.snp.makeConstraints { (make) in
            make.top.equalTo(electricianNameTextfield.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        signDateTextfield.textField.isEnabled = false
        signDateTextfield.textField.clearButtonMode = .never
        backView.addSubview(signDateTextfield)
        signDateTextfield.snp.makeConstraints { (make) in
            make.top.equalTo(dateTitle.snp.bottom).offset(4)
            make.left.right.equalTo(certificateOfLabel)
            make.height.greaterThanOrEqualTo(0)
        }
        //
        dateCalendarBtn.setImage(UIImage(named: "electrical_certificate_date_sel"), for: UIControl.State.normal)
        backView.addSubview(dateCalendarBtn)
        dateCalendarBtn.snp.makeConstraints { (make) in
            make.right.centerY.equalTo(signDateTextfield)
            make.width.height.equalTo(40)
        }
        //signature
        let signatureTitle = ContentLabel()
        signatureTitle.text = "Signature"
        signatureTitle.font = titleFont
        backView.addSubview(signatureTitle)
        signatureTitle.snp.makeConstraints { (make) in
            make.top.equalTo(signDateTextfield.snp.bottom).offset(14)
            make.left.equalTo(certificateOfLabel)
            make.width.height.greaterThanOrEqualTo(0)
        }
        //
        let signatureViewBackView = UIView()
        signatureViewBackView.backgroundColor = .white
        signatureViewBackView.layer.borderWidth = 1
        signatureViewBackView.layer.borderColor = UIColor(red: 0.9, green: 0.92, blue: 0.93, alpha: 1).cgColor
        signatureViewBackView.layer.cornerRadius = Constant.StandardCornerRadius
        signatureViewBackView.clipsToBounds = true
        backView.addSubview(signatureViewBackView)
        signatureViewBackView.snp.makeConstraints { (make) in
            make.top.equalTo(signatureTitle.snp.bottom).offset(12)
            make.left.right.equalTo(certificateOfLabel)
            make.height.equalTo(240)
        }
        signatureView.backgroundColor = .white
        signatureViewBackView.addSubview(signatureView)
        signatureView.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
        //
        clearSignatureBtn.setTitle(R.string.localizable.clear(), for: UIControl.State.normal)
        clearSignatureBtn.setTitleColor(UIColor(hexStr: Constant.ThemeColorHexStr), for: UIControl.State.normal)
        clearSignatureBtn.setFont(R.font.interMedium(size: 15))
        signatureViewBackView.addSubview(clearSignatureBtn)
        clearSignatureBtn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(signatureView)
            make.height.equalTo(40)
            make.width.equalTo(80)
        }
        //cancel btn
        backView.addSubview(cancelBtn)
        cancelBtn.snp.makeConstraints { (make) in
            make.top.equalTo(signatureView.snp.bottom).offset(15)
            make.left.equalTo(16)
            make.height.equalTo(OperationButton.height)
            make.bottom.equalToSuperview().offset(-10)
            
        }
        //add btn
        backView.addSubview(addBtn)
        addBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(cancelBtn)
            make.left.equalTo(cancelBtn.snp.right).offset(20)
            make.right.equalTo(-16)
            make.size.equalTo(cancelBtn)
        }
        clearSignatureBtn.addTarget(self, action: #selector(clearSignature), for: UIControl.Event.touchUpInside)
        //显示picker
        dateOfTestCalendarBtn.tag = 1
        //
        dateCalendarBtn.tag = 2
    }
    func setData(_ pvdModel: PVDDetailModel){
        let model = DBManager.shared.getESCInfo()
        let ownerInfo = pvdModel.owner_address
        //installation flag
        if let data = model.installation_flag, data{
            self.installationCheckbox.check()
        }
        //equipment flag
        if let data = model.equipment_flag, data{
            self.equipmentCheckbox.check()
        }
        //owner title
        if let data = model.owner_name_title, data != ""{
            self.nameTitleTextfield.textField.text = data
        }
        //owner given name / first name
        if let data = model.owner_given_name, data != ""{
            self.givenNameTextfield.textField.text = data
        }else{
            if pvdModel.owner_address.type.lowercased() == "individual" {
                if ownerInfo.first_name != ""{
                    self.givenNameTextfield.textField.text = ownerInfo.first_name
                }
            }else{//
                if ownerInfo.type_name != ""{
                    self.givenNameTextfield.textField.text = ownerInfo.type_name
                }
            }
        }
        //owner last name
        if let data = model.owner_surname, data != ""{
            self.surnameTextfield.textField.text = data
        }else if pvdModel.owner_address.type.lowercased() == "individual"{
            self.surnameTextfield.textField.text = ownerInfo.last_name
        }
        //installation address street name
        if let data = model.installation_address_street_name , data != ""{
            self.streetTextfield.textField.text = data
        }else{
            var combineStreetName = ""
            let installationAddress = pvdModel.install_address
            //unit type
            if installationAddress.unit_type != ""{
                combineStreetName = installationAddress.unit_type
            }
            //unit number
            if installationAddress.unit_number != ""{
                combineStreetName = (combineStreetName == "" ? installationAddress.unit_number : combineStreetName + " " + installationAddress.unit_number)
            }
            //street number
            if installationAddress.street_number != ""{
                combineStreetName = (combineStreetName == "" ? installationAddress.street_number : combineStreetName + " " + installationAddress.street_number)
            }
            //street name
            if installationAddress.street_name != ""{
                combineStreetName = (combineStreetName == "" ? installationAddress.street_name : combineStreetName + " " + installationAddress.street_name)
            }
            //street type
            if installationAddress.street_type != ""{
                combineStreetName = (combineStreetName == "" ? installationAddress.street_type : combineStreetName + " " + installationAddress.street_type)
            }
            self.streetTextfield.textField.text = combineStreetName
            
        }
        //installation address suburb
        if let data = model.installation_address_suburb, data != ""{
            self.suburbTextfield.textField.text = data
        }else{
            self.suburbTextfield.textField.text = pvdModel.install_address.suburb
        }
        //installation address postcode
        if let data = model.installation_address_postcode, data != ""{
            self.postcodeTextfield.textField.text = data
        }else{
            self.postcodeTextfield.textField.text = pvdModel.install_address.post_code
        }
        //installation equipment tested description
        if let data = model.installation_equipment_tested_descrip, data != ""{
            self.electricalTestTextView.textView.text = data
        }else {
            let systemDetail = pvdModel.system_detail
            //panel
            var combineStr = ""
            for temp in systemDetail.panels{
                combineStr += "\(temp.number_panels )" + " * " + (temp.model) + "; "
            }
            if combineStr.count > 1{
                combineStr = (combineStr as NSString).substring(to: (combineStr as NSString).length - 2)
            }
            combineStr += " installed total system size is " + systemDetail.rated_output + "kW\n"
            //inverter
            for temp in systemDetail.inverters{
                combineStr += "\(temp.number_inverters)" + " * " + (temp.inverter_model) + "; "
            }
            if combineStr.count > 1{
                combineStr = (combineStr as NSString).substring(to: (combineStr as NSString).length - 2) +  " installed"
            }
            self.electricalTestTextView.textView.text = combineStr
        }
        //date of test
        if let data = model.date_of_test, data != ""{
            self.dateOfTestTextfield.textField.text = data
        }
        //contractor_licence_number
        if let data = model.contractor_licence_number, data != ""{//0.用保存过的数据
            self.electricalContractorLicenceTextfield.textField.text = data
        }else if pvdModel.company_electrical_contractor_license_number != ""{//1.Retailer admin在System setup中设置的默认值
            self.electricalContractorLicenceTextfield.textField.text = pvdModel.company_electrical_contractor_license_number
        }else  {//2.使用签名中的数据
            self.electricalContractorLicenceTextfield.textField.text = pvdModel.signatures.electrician_licenses
        }
        
        //contractor_licence_name
        if let data = model.contractor_licence_name, data != ""{//0.用保存过的数据
            self.nameOnContractorLicenceTextfield.text = data
        }else if pvdModel.company_electrical_contractor_Name != ""{//1.Retailer admin在System setup中设置的默认值
            self.nameOnContractorLicenceTextfield.text = pvdModel.company_electrical_contractor_Name
        }else  {//2.使用签名中的数据
            self.nameOnContractorLicenceTextfield.text = pvdModel.signatures.electrician_full_name
        }
        //contractor_licence_phone
        if let data = model.contractor_licence_phone, data != ""{//0.用保存过的数据
            self.phoneOnContractorLicenceTextfield.text = data
        }else if pvdModel.company_electrical_contractor_phone_number != ""{//1.Retailer admin在System setup中设置的默认值
            self.phoneOnContractorLicenceTextfield.text = pvdModel.company_electrical_contractor_phone_number
        }else {//2.使用签名中的数据
            self.phoneOnContractorLicenceTextfield.text = pvdModel.signatures.electrician_phone
        }
        //electrician name
        if let data = model.electrician_name, data != ""{
            self.electricianNameTextfield.text = data
        }else {
            self.electricianNameTextfield.text = pvdModel.signatures.electrician_full_name
        }
        //sign date
        if let data = model.sign_date, data != ""{
            self.signDateTextfield.textField.text = data
        }else{
            self.signDateTextfield.textField.text = DateUtil().getDatetimeStr(Date(), format: "dd/MM/yyyy")
        }
    }
    func getAllFieldData()->ESCInfoModel{
        let model = ESCInfoModel()
        if self.installationCheckbox.tag == 1{
            model.installation_flag = true
        }
        if self.equipmentCheckbox.tag == 1{
            model.equipment_flag = true
        }
        //
        if let data = self.nameTitleTextfield.textField.text, data != ""{
            model.owner_name_title = data
        }
        if let data = self.givenNameTextfield.textField.text, data != ""{
            model.owner_given_name = data
        }
        if let data = self.surnameTextfield.textField.text, data != ""{
            model.owner_surname = data
        }
        if let data = self.streetTextfield.textField.text, data != ""{
            model.installation_address_street_name = data
        }
        if let data = self.suburbTextfield.textField.text, data != ""{
            model.installation_address_suburb = data
        }
        if let data = self.postcodeTextfield.textField.text, data != ""{
            model.installation_address_postcode = data
        }
        if let data = self.electricalTestTextView.textView.text, data != ""{
            model.installation_equipment_tested_descrip = data.trimmingCharacters(in: .whitespacesAndNewlines)
        }
        if let data = self.dateOfTestTextfield.textField.text, data != ""{
            model.date_of_test = data
        }
        if let data = self.electricalContractorLicenceTextfield.textField.text, data != ""{
            model.contractor_licence_number = data
        }
        if let data = self.nameOnContractorLicenceTextfield.text, data != ""{
            model.contractor_licence_name = data
        }
        if let data = self.phoneOnContractorLicenceTextfield.text, data != ""{
            model.contractor_licence_phone = data
        }
        if let data = self.electricianNameTextfield.text, data != ""{
            model.electrician_name = data
        }
        if let data = self.signDateTextfield.textField.text, data != ""{
            model.sign_date = data
        }
        return model
    }
    
    @objc func clearSignature(){
        self.signatureView.clearSignature()
    }
    //签名区域拦截UIScrollView的滑动事件
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        //转换当前点击的坐标为signatureview的坐标系中
        let poinitOnSignView = self.convert(point, to: self.signatureView)
        //转换当前点击的坐标为clearSignatureBtn的坐标系中
        let poinitOnClearSignBtn = self.convert(point, to: self.clearSignatureBtn)
        
        if self.signatureView.point(inside: poinitOnSignView, with: event) {//如果点击的点在签名区域且不再Clear区域，则scrollview不能滚动
            if self.clearSignatureBtn.point(inside: poinitOnClearSignBtn, with: event){//点击点在Clear button，则有clear button处理
                return self.clearSignatureBtn
            }else{//否则可以签名处理滑动事件
                self.scrollView.isScrollEnabled = false
                return self.signatureView
            }
        }else{//如果点不在签名区域，则scrollview滚动
            self.scrollView.isScrollEnabled = true
            return super.hitTest(point, with: event)
        }
    }
}
